package net.tycmc.cty.ctyms.timejob.iems.dao;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.tycmc.functions.util.MapGetter;

import net.tycmc.bulb.common.dao.IBaseDAO;
import net.tycmc.bulb.common.util.DateUtil;
import net.tycmc.bulb.dbutil.global.GlobalDBVar;
import net.tycmc.cty.ctyms.timejob.common.MyConnectDSDAO;

public class MaintAlertsDAO {
	private static Logger logdata = Logger.getLogger("mtnAlertsLog");
	private IBaseDAO baseDAO;
	public void setBaseDAO(IBaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}
	/**
	 * 查询系统参数保养标准工作小时数、里程
	 * @author jiyongtian 20140530 IEMS3.6 新增
	 **/
	public Map<String, Object> querySysParmConfig() {
		String strSQL = "select SysPC_MtnInHours,SysPC_MtnInKm from Sys_ParmConfig";
		return this.baseDAO.execQueryOne(strSQL, new Object[]{});
	}
	/**
	 * 固定时间间隔保养上次报警时间
	 * @author jiyongtian 20140530 IEMS3.6 新增
	 **/
	public List<Map<String, Object>> queryPreMtnGapAlarmInfo() {
		String strSQL = "select MtnAN_VclID,CONVERT(varchar(10),MtnAN_AlarmDate,121) MtnAN_AlarmDate from Mtn_Alarm_InfoNew mai inner join V_VehicleInfo vv on mai.MtnAN_VclID = vv.VehicleID " +
				"where MtnAN_IsGapForst = 1 and MtnAN_AlarmDate is not null and MtnAN_ID in (select MAX(MtnAN_ID) from Mtn_Alarm_InfoNew group by MtnAN_VclID)" +
				"and vv.UsefulLife>= GETDATE() and vv.Tmnl_SoftEdition is not null and vv.Tmnl_SoftEdition <> '' ";
		return this.baseDAO.execQuery(strSQL, new Object[]{});
	}
	/**
	 * 此次保养提示处理的设备信息集
	 * @author jiyongtian 20140530 IEMS3.6 新增
	 **/
	public List<Map<String, Object>> queryMaintAlertsVclsInfo() {
		StringBuffer strBuf =new StringBuffer();
		String startMonth=DateUtil.addDay(-1).substring(0, 7).replace("-", "");
		String sql=" ( select MsgESS_Vcl_ID,"+
				"MsgESS_MsgTime,"+
				"MsgESS_iWork,"+
				"MsgESS_tWork,"+
				"MsgESS_iOilCons,"+
				"MsgESS_tOilCons,"+
				"MsgESS_iRunKmECM,"+
				"MsgESS_tRunKmECM,"+
				"MsgESS_iRunKmGPS,"+
				"MsgESS_tRunKmGPS,"+
				"MsgESS_iRunKmExact,"+
				"MsgESS_tRunKmExact,"+
				"MsgESS_iRunKmExact_ECM,"+
				"MsgESS_tRunKmExact_ECM,"+
				"MsgESS_iRunKmECU,"+
				"MsgESS_tRunKmECU,"+
				"MsgESS_iIdleWorkTime_PGN,"+
				"MsgESS_tIdleWorkTime_PGN,"+
				"MsgESS_iIdleWorkTime_UDS,"+
				"MsgESS_tIdleWorkTime_UDS,"+
				"MsgESS_iUreaConsumption,"+
				"MsgESS_tUreaConsumption "+
				"from "+GlobalDBVar.getDBFullName_Conn("Msg_EquipmentState_Statistics_"+startMonth, "3", null)+")";
		strBuf.append("select vv.VehicleNumber,vv.DBCode,vv.VehicleTypeID,vv.VehicleID,ms.MtnFS_MtnIn,ms.MtnFS_MtnOut,MtnFS_MtnType,MaintfixeStartDate,mst.MsgESS_tWork as MsgSTP_tEWorkHours,");
		strBuf.append("tRunKm = case ISNULL(vv.VehicleRumKmID,1) when 1 then ISNULL(MsgESS_tRunKmExact,0) when 2 then ISNULL(MsgESS_tRunKmExact_ECM,0) when 3 then ISNULL(MsgESS_tRunKmECM,0) when 4 then ISNULL(MsgESS_tRunKmECU,0) when 5 then ISNULL(MsgESS_tRunKmGPS,0) end  ");
		strBuf.append("from V_VehicleInfo vv inner join MtnForst_StandardNew ms on vv.VehicleTypeID = ms.MtnFS_VclT_ID inner join "+sql+" mst on mst.MsgESS_Vcl_ID = vv.VehicleID ");
		strBuf.append("where 1=1 and mst.MsgESS_MsgTime = '"+DateUtil.addDay(-1)+"' and vv.UsefulLife > GETDATE() and vv.Tmnl_SoftEdition is not null and vv.Tmnl_SoftEdition <> '' and  GETDATE() between ms.MtnFS_Eff  and  ms.MtnFS_Exp ");
		return this.baseDAO.execQuery(strBuf.toString(), new Object[]{});
	}
	/**
	 * 校验保养方式为公里数、小时数时是否已提示,如已提示返回true
	 * @author jiyongtian 20140530 IEMS3.6 新增
	 **/
	public boolean checkMaintOfKmOrTime(int mtnType, String vehicleID,
			String maintDetail,int count) {
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("select Count(1) as counts from Mtn_Alarm_InfoList where MtnAL_VclID = '"+vehicleID+"' ");
		strBuf.append("and MtnAL_AlarmValue = '"+maintDetail+"' and MtnAL_AlarmType = '"+mtnType+"' and MtnAL_AlarmTimes = '"+count+"'");
		Map<String,Object> map = this.baseDAO.execQueryOne(strBuf.toString(), new Object[]{});
		if(Integer.parseInt((String)MapGetter.getString(map, "counts"))>0){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 录入保养计划提醒信息列表
	 * @author jiyongtian 20140530 IEMS3.6 新增
	 **/
	public boolean InsertMaintList(Map<String, Object> oneVcl, String armvalue,
			int count, int mtnType,String isbakTable) {
		MyConnectDSDAO  myConnectDSDAO=new MyConnectDSDAO();
		String sql = "insert into Mtn_Alarm_InfoList(MtnAL_VclT_ID,MtnAL_VclID,MtnAL_AlarmDate,MtnAL_AlarmValue,MtnAL_AlarmType,MtnAL_AlarmTimes)values(?,?,?,?,?,?)";
		logdata.info("insert into Mtn_Alarm_InfoList(MtnAL_VclT_ID,MtnAL_VclID,MtnAL_AlarmDate,MtnAL_AlarmValue,MtnAL_AlarmType,MtnAL_AlarmTimes)values('"+MapGetter.getString(oneVcl, "VehicleTypeID")+"','"+MapGetter.getString(oneVcl, "VehicleID")+"','"+DateUtil.addDay(0)+"','"+armvalue+"',"+mtnType+"','"+count+")");
		int flag = myConnectDSDAO.execInsert(sql, new Object[]{MapGetter.getString(oneVcl, "VehicleTypeID"),MapGetter.getString(oneVcl, "VehicleID"),DateUtil.addDay(0),armvalue,mtnType,count});
		//二期优化：是否需要备份数据
		if(isbakTable.equals("1")){
			MyConnectDSDAO conn = new MyConnectDSDAO(true);
			int flager =conn.execInsert(sql, new Object[]{MapGetter.getString(oneVcl, "VehicleTypeID"),MapGetter.getString(oneVcl, "VehicleID"),DateUtil.addDay(0),armvalue,mtnType,count});
			if(flager<=0){
				System.out.println("录入保养计划提醒信息列表备份失败！！");
			}
		}
		if(flag>0){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 保养提示信息录入 提示信息表
	 * @author jiyongtian 20140530 IEMS3.6 新增
	 **/
	public void InsertPromtOfMaint(Map<String, Object> vclInfo, String users,
			int mtnType) {
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("insert into Msg_Prompt(MsgP_VclID,MsgP_MsgID,MsgP_MsgCID,MsgP_MsgTime,MsgP_Type,MsgP_State,MsgP_DBName,MsgP_TableName,MsgP_UpdateTime,MsgP_CreateTime,MsgP_DetailMsg,MsgP_SMSRecUser,MsgP_SecTypeId,MsgP_SecTypeName)");
		strBuf.append("values(?,IDENT_CURRENT('Mtn_Alarm_InfoList'),IDENT_CURRENT('Mtn_Alarm_InfoList'),?,3,5,'M','Mtn_Alarm_InfoList',GETDATE(),GETDATE(),?,?,?,?);");
		String secTypeName = "";
		switch(mtnType){
		case 1:
			//secTypeName = "工作小时数";
			secTypeName = "小时#保养";
			break;
		case 2:
			//secTypeName = "公里数";
			secTypeName = "公里#保养";
			break;
		case 3:
			//secTypeName = "固定时间间隔";
			secTypeName = "天#保养";
			break;
		default:break;
		}
		String detailMsg = MapGetter.getString(vclInfo, "VehicleNumber")+"，#"+MapGetter.getString(vclInfo, "armvalue")+secTypeName;
		this.baseDAO.execInsert(strBuf.toString(), new Object[]{
			MapGetter.getString(vclInfo, "VehicleID"),DateUtil.addDay(0),
			detailMsg,users,mtnType,secTypeName
		});
	}
	/**
	 * 根据保养计划提醒信息列表   录入保养计划提醒信息表
	 * @author jiyongtian 20140530 IEMS3.6 新增
	 **/
	public boolean InsertMaintAlarm(String isbakTable) {
		MyConnectDSDAO myConnectDSDAO=new MyConnectDSDAO();
		String AlarmDate = DateUtil.addDay(0);
		StringBuffer strTime = new StringBuffer();
		strTime.append("insert into Mtn_Alarm_InfoNew(MtnAN_VclT_ID,MtnAN_VclID,MtnAN_AlarmDate,MtnAN_IsWorkForst,MtnAN_WorkAlarm) ");
		strTime.append("select MtnAL_VclT_ID,MtnAL_VclID,MtnAL_AlarmDate,1,MtnAL_AlarmValue from Mtn_Alarm_InfoList where MtnAL_AlarmType = 1 and MtnAL_AlarmDate = '"+AlarmDate+"' ");
		
		StringBuffer strKm = new StringBuffer();
		strKm.append("insert into Mtn_Alarm_InfoNew(MtnAN_VclT_ID,MtnAN_VclID,MtnAN_AlarmDate,MtnAN_IsKmForst,MtnAN_KmAlarm) ");
		strKm.append("select MtnAL_VclT_ID,MtnAL_VclID,MtnAL_AlarmDate,1,MtnAL_AlarmValue from Mtn_Alarm_InfoList where MtnAL_AlarmType = 2 and MtnAL_AlarmDate = '"+AlarmDate+"' ");
		
		StringBuffer strDay = new StringBuffer();
		strDay.append("insert into Mtn_Alarm_InfoNew(MtnAN_VclT_ID,MtnAN_VclID,MtnAN_AlarmDate,MtnAN_IsGapForst,MtnAN_GapAlarm) ");
		strDay.append("select MtnAL_VclT_ID,MtnAL_VclID,MtnAL_AlarmDate,1,MtnAL_AlarmValue from Mtn_Alarm_InfoList where MtnAL_AlarmType = 3 and MtnAL_AlarmDate = '"+AlarmDate+"' ");
		
		int i=myConnectDSDAO.execInsert(strTime.toString(), new Object[]{});
		int j=myConnectDSDAO.execInsert(strKm.toString(), new Object[]{});
		int k=myConnectDSDAO.execInsert(strDay.toString(), new Object[]{});
		//二期优化：是否需要备份数据
		if(isbakTable.equals("1")){
			MyConnectDSDAO conn=new MyConnectDSDAO(true);
			int f=conn.execInsert(strTime.toString(), new Object[]{});
			int q=conn.execInsert(strKm.toString(), new Object[]{});
			int w=conn.execInsert(strDay.toString(), new Object[]{});
			if(f<=0){
				System.out.println("录入保养计划提醒表备份失败！！：strTime！");
			}
			if(q<=0){
				System.out.println("录入保养计划提醒表备份失败！！：strKm！");
			}
			if(w<=0){
				System.out.println("录入保养计划提醒表备份失败！！：strDay！");
			}
		}
		if(i>0&&j>0&&k>0){
			return true;
		}else{
			return false;
		}
	}
}